package dbEngin

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"github.com/go-xorm/core"
	"hotel_Manager_Api/util"
)

var engine *xorm.Engine

func createEnging() *xorm.Engine{
	if engine==nil {
		var err error
		dbIp := util.Config().MustValue("db","server");
		dbPort := util.Config().MustValue("db","port","3306")
		dbUserName := util.Config().MustValue("db","userName")
		dbPwd := util.Config().MustValue("db","password")
		dbName := util.Config().MustValue("db","db_name")
		connStr := dbUserName+":"+dbPwd+"@tcp("+dbIp+":"+dbPort+")/"+dbName+"?charset=utf8"
		fmt.Println(connStr)
		engine, err = xorm.NewEngine("mysql", connStr)
		engine.ShowSQL(util.Config().MustBool("db","show_sql",false))
		engine.Logger().SetLevel(core.LOG_DEBUG)
		engine.SetMapper(core.GonicMapper {})
		fmt.Print(err)
	}
	return engine;
}

func GetEnging()*xorm.Engine  {
	return createEnging();
}
